package upload

import (
	"bufio"
	"github.com/sirupsen/logrus"
	"io"
	"os"
	"strings"
)

var (
	mimes map[string]string
)

func init()  {
	dir, _ := os.Getwd()

	mimes = make(map[string]string)

	f, err := os.Open(dir + "/data/mime_ext.txt")
	if err != nil {
		logrus.Panicf("open [data/mime_ext.txt] error: %v", err)
	}
	defer f.Close()

	rd := bufio.NewReader(f)
	for {
		line, err := rd.ReadString('\n')
		if err != nil || io.EOF == err {
			break
		}
		temps := strings.Split(strings.TrimSpace(line), " ")

		mimes[temps[1]] = temps[0]
	}
}

// MimeToExt mime转为后缀名，后缀名有.
func MimeToExt(mime string) string {
	return mimes[mime]
}
